package com.rj.framework.context;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 全局上下文实现
 */
public class DefaultGlobalContext implements GlobalContext {
    private final Map<String, Object> sharedData = new ConcurrentHashMap<>();
    private final UserContext userContext;

    public DefaultGlobalContext(UserContext userContext) {
        this.userContext = userContext;
    }

    @Override
    public <T> T getSharedData(String key, Class<T> type) {
        Object value = sharedData.get(key);
        return value != null ? type.cast(value) : null;
    }

    @Override
    public void setSharedData(String key, Object value) {
        sharedData.put(key, value);
    }

    @Override
    public UserContext getUserContext() {
        return userContext;
    }
}